package by.bsu.scs.util.form;

import java.io.File;
import java.util.ArrayList;

import org.apache.struts.util.LabelValueBean;

import by.bsu.scs.db.dao.DisciplineInstructorHome;
import by.bsu.scs.db.dao.DisciplineTaskHome;
import by.bsu.scs.db.dao.TaskAttachmentHome;
import by.bsu.scs.db.dao.TaskHome;
import by.bsu.scs.model.DisciplineInstructor;
import by.bsu.scs.model.Task;
import by.bsu.scs.model.TaskAttachment;
import by.bsu.scs.model.User;
import by.bsu.scs.presentation.form.LiteratureForm;
/**
 * 
 * @author Svetlana Voiteh
 * @version 1.0
 *
 * Fill the elements of LiteratureForm
 */
public class LiteratureFormFiller {

	private LiteratureForm form;
	private User student;

	public LiteratureFormFiller(LiteratureForm form, User student) {
		super();
		this.form = form;
		this.student = student;
	}

	/**
	 * Fill the elements of LiteratureForm
	 */
	public void fillElements() {
		ArrayList<DisciplineInstructor> disciplineInstructorList = new ArrayList<DisciplineInstructor>();
		ArrayList<TaskAttachment> literatureList = new ArrayList<TaskAttachment>();
		ArrayList<LabelValueBean> disciplineList = new ArrayList<LabelValueBean>();
		ArrayList<Literature> literatures = new ArrayList<Literature>();

		disciplineInstructorList = (ArrayList<DisciplineInstructor>) (new DisciplineInstructorHome())
				.getDisciplineInstructorByStudentIdSQL(this.student);

		/*
		 *  fill list of disciplines
		 */
		for (DisciplineInstructor di : disciplineInstructorList) {
			disciplineList.add(new LabelValueBean(di.getDiscipline()
					.getDisciplineName(), new Integer(di.getDiscipline()
					.getDisciplineId()).toString()));
		}

		ArrayList<Task> taskList = new ArrayList<Task>();
		/*
		 *  fill list of tasks
		 */
		if (this.form.getDisciplineId() == 0) {
			taskList = (ArrayList<Task>) new TaskHome()
					.getTasksByStudentIdSQL(this.student);
		}
		/*
		 *  if update
		 */
		else {
			taskList = (ArrayList<Task>) new DisciplineTaskHome()
					.getTasksByStudentAndDisciplineIdSQL(this.student,
							this.form.getDisciplineId());
		}

		ArrayList<TaskAttachment> temp = new ArrayList<TaskAttachment>();
		/*
		 *  fill list of literature
		 */
		for (Task t : taskList) {

			temp = (ArrayList<TaskAttachment>) new TaskAttachmentHome()
					.getTaskAttachmentByStudentTaskAndState(this.student, t);
			literatureList.addAll(temp);
		}

		for (TaskAttachment ta : literatureList) {
			literatures.add(new Literature(ta.getTask().getTaskTitle(),
					new File(ta.getTaskAttachmentName())));
		}

		this.form.setDisciplineInstructorList(disciplineInstructorList);
		this.form.setDisciplineList(disciplineList);
		this.form.setLiteratures(literatures);

	}
}
